<?php
		  
	include ("Outils.inc");
	require ("connect.php");	
		  
	if ( !isset($_COOKIE['identite']) ) {
		  $Nom = ($_POST['Nom']);
		  $Code = ($_POST['Code']);
		  $mois = 60 * 60 * 24 * 30;

		 identification($Nom, $Code, $serv, "IdentificationC.php");
		 //la fonction identification quitte si l'identification a �chou�
		 
		  	setcookie('identite[0]', "$Nom", time()+ $mois);
			setcookie('identite[1]', "$Code", time()+ $mois);
			$cookieEnvoye = true;
	}
	else {
		$cookieEnvoye = true;
		$identite = $_COOKIE['identite'];
		$Nom = $identite[0];
		$Code = $identite[1];

	}
		  
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="style.css" rel="stylesheet" type="text/css">
<style type="text/css" media="screen">@import "style.css";</style>
<title> VideoExpress - PHP Projet</title></head>

<body>
<div class="center">
<div class="outer" id="outer">
<div class="outer_l">
<div class="outer_r">

<!--ENTETE & MENU-->
<?php 

banniere("VideoExpress","Nguyen Tat Son Tung");
?>
<!--FIN ENTETE & MENU-->


<!--CONTENT-->
<div class="wrap main_content_max" id="main_content">
<div class="content">
<table class="blog" cellpadding="0" cellspacing="0">
<tr><td valign="top"><div>
		
<table class="contentpaneopen"><tr>
<td class="contentheading" width="100%">COMMANDE DE CASSETTES</td></tr></table>
<table class="contentpaneopen"><tr><td valign="top" colspan="2">
<?php
require ("connect.php");


/*$Code=$_COOKIE["Code"];
$Nom=$_COOKIE["Nom"];*/
$MAX=$_POST["Max"];
$nbfilms=0;
$nbchecked=0; 
echo "<table><tr><td><img src='images/text.png'><br \></td>
	<td><div align='right'>
	<form method='POST' action='Deconnecte.php' \">
Deconnect� <input type='image' src='images/deconnect.ico' alt='Deconnecte'>
</form></td></tr></table><br \>"; 
echo "<br \>
	<div class='jl_table'>
  	<table cellpadding='3' cellspacing='0' border='1'>
  	<tr class='sectiontableheader'>
	<td><font size='2'>Choix</font></td>
	<td><font size='2'>NoFilm</font></td>
	<td><font size='2'>Titre</font></td>
	<td><font size='2'>Disponible</font></td>
	</tr>
	<form method='POST' action='ExecuterCommande.php'>
	<input type='HIDDEN' class='input' name='Code' value='$Code'/>
	<input type='HIDDEN' class='input' name='Nom' value='$Nom'/>";
	

for ($i=1;$i<=$MAX;$i++){

if ( isset($_POST["NoFilm$i"]) && ( $_POST["NoFilm$i"] != "" )){

$Support=$_POST["Support$i"];
$NoFilm=$_POST["NoFilm$i"];

$nbfilms++;

$req1="SELECT *
	FROM `films`
	WHERE `NoFilm` LIKE '$NoFilm'";
$req2="SELECT *
	FROM `cassettes` `c`
	WHERE `c`.`NoFilm` LIKE '$NoFilm'
	AND `c`.`Support` LIKE '$Support'
	AND ( `c`.`Statut` LIKE 'disponible' OR ( `c`.`Statut` LIKE 'reservee' AND EXISTS (SELECT * FROM `empres` `e` WHERE `c`.`NoFilm` = `e`.`NoFilm` AND `c`.`NoExemplaire` = `e`.`NoExemplaire` AND (TIMEDIFF(NOW(), `DateEmpRes`) > '00:00:10' ) OR ( TIMEDIFF(NOW(), `DateEmpRes`) < '00:00:10' AND `e`.`CodeAbonne` = '$Code' ) )))";
$req3="SELECT *
	FROM `cassettes` `c`
	WHERE `c`.`NoFilm` LIKE '$NoFilm'
	AND ( `c`.`Statut` LIKE 'disponible' OR ( `c`.`Statut` LIKE 'reservee' AND EXISTS (SELECT * FROM `empres` `e` WHERE `c`.`NoFilm` = `e`.`NoFilm` AND `c`.`NoExemplaire` = `e`.`NoExemplaire` AND (TIMEDIFF(NOW(), `DateEmpRes`) > '00:00:10' ) OR ( TIMEDIFF(NOW(), `DateEmpRes`) < '00:00:10' AND `e`.`CodeAbonne` = '$Code' ) )))";


$resul1 = DB_execSQL($req1,$serv);
$resul2 = DB_execSQL($req2,$serv);
$resul3 = DB_execSQL($req3,$serv);
$count1 = mysql_num_rows($resul1);
$count2 = mysql_num_rows($resul2);
$count3 = mysql_num_rows($resul3);

if ( $count1 == 0 ) {
$dispo="Non";
echo "<TR><TD><h2>NumFilm$i
	<TD><h2>$NoFilm
	<TD><h2>Ce film n'a pas trouv�
	<TD><h2>$dispo
	</TR>";
} else { 
	if( $count3 == 0 ){
		while ($film = mysql_fetch_assoc($resul1)) {
		$dispo="Non";
		$NoFilm=$film['NoFilm'];
		$Titre=$film['Titre'];
		echo "<TR><TD><h2>NumFilm$i
			<TD><h2>$NoFilm
			<TD><h2>$Titre
			<TD><h2>$dispo
			</TR>";
		}
		} else {
			if ( $count2 == 0 ){
			while (($film = mysql_fetch_assoc($resul1)) && ( $trouve = mysql_fetch_assoc($resul3))){
			$NoFilm=$film['NoFilm'];
			$Titre=$film['Titre'];
			$Support=$trouve['Support'];
			$NoExemplaire=$trouve['NoExemplaire'];
			$dispo="Oui";
			echo "<TR><TD><h2>NumFilm$i  <input type='checkbox' name='Num$i' value='$i' name='Num$i' />
			<TD><h2>$NoFilm
			<TD><h2>$Titre
			<TD><h2>$dispo disponible en $Support
			<input type='HIDDEN' class='input' name='Exi$i' value='$NoExemplaire'/>
			<input type='HIDDEN' class='input' name='NoFilm$i' value='$NoFilm'/>
			</TR>";
			$req4="UPDATE `cassettes`
			SET `Statut` = 'reservee'
			WHERE `NoFilm` LIKE '$NoFilm'
			AND `NoExemplaire` LIKE '$NoExemplaire'
			AND `Support` LIKE '$Support'";
			DB_execSQL($req4,$serv);
			$req5="INSERT INTO `empres` ( `NoFilm` , `NoExemplaire` , `CodeAbonne` , `DateEmpRes` )"
	."VALUES ( '$NoFilm' , '$NoExemplaire' , '$Code' , NOW())";
			DB_execSQL($req5,$serv);
			$nbchecked++;
			}
			mysql_free_result($resul1);
			mysql_free_result($resul3);
			}else{
			while (($support = mysql_fetch_assoc($resul2)) && ($film = mysql_fetch_assoc($resul1))){
			$NoFilm=$film['NoFilm'];
			$Titre=$film['Titre'];
			$Support=$support['Support'];
			$NoExemplaire=$support['NoExemplaire'];
			$dispo="Oui";
			echo "<TR><TD><h2>NumFilm$i  <input type='checkbox' name='Num$i' value='$i' checked='checked' />
			<TD><h2>$NoFilm
			<TD><h2>$Titre 
			<TD><h2>$dispo en $Support
			<input type='HIDDEN' class='input' name='Exi$i' value='$NoExemplaire'/>
			<input type='HIDDEN' class='input' name='NoFilm$i' value='$NoFilm'/>
			</TR>";
			$req4="UPDATE `cassettes`
			SET `Statut` = 'reservee'
			WHERE `NoFilm` LIKE '$NoFilm'
			AND `NoExemplaire` LIKE '$NoExemplaire'
			AND `Support` LIKE '$Support'";
			DB_execSQL($req4,$serv);
			$req5="INSERT INTO `empres` ( `NoFilm` , `NoExemplaire` , `CodeAbonne` , `DateEmpRes` )"
			."VALUES ( '$NoFilm' , '$NoExemplaire' , '$Code' , NOW())";
			DB_execSQL($req5,$serv);
			$nbchecked++;
			}
			mysql_free_result($resul1);
			mysql_free_result($resul2);	
			}
		
}
}
}
}

echo "</table>
	</div>";
	


if ( $nbchecked != 0 ) {
echo "<p><input type='submit' class='button' value='Commander' STYLE='font-family:Garamond; font-size:medium;
font-style:normal; background:#444444 none; color:#FFF; border: 1px dotted'>";
echo "</form>";
echo "<h2>Attention : La r�servation n'est valable que pendant 10 secondes</h2>";
}else{
echo "<font size='2'>Vous avez choisi aucun film disponible</font>";
echo "</form>";
}
echo "<form method='POST' action='Commande.php'>
	<input type='HIDDEN' class='input' name='Code' value='$Code'/>
	<input type='HIDDEN' class='input' name='Nom' value='$Nom'/>
	<p><input type='submit' name='Submit' class='button' value='Revoir le choix' STYLE='font-family:Garamond; font-size:medium;
font-style:normal; background:#444444 none; color:#FFF; border: 1px dotted'>";

?>
</td></tr></table>
<span class="article_separator">&nbsp;</span>
</div></td></tr></table>
</div>
</div>
</div>
</div>
</div>
</div>
<!--FIN CONTENT-->

<!-- PIED -->
<?php include ("inc/inc-pied.php"); ?>
<!-- FIN - PIED --> 
</body>
</html>